package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Service("userService")
@Scope("singleton")
public class UserServiceImpl implements UserService {

    // 非引用类型属性注入
    @Value("666")
    private int num;
    @Value("v1.0.0")
    private String version;

    // 引用类型属性注入
    @Autowired  //按类型匹配，指定@Qualifier后可以指定自动装配的bean的id
    @Qualifier("userDao1")
    private UserDao userDao;
    @Autowired
    private BookDao bookDao;

    public void save() {
        System.out.println("user service running..."+num+" "+version);
        userDao.save();
        bookDao.save();
    }

    // 初始化方法：singleton仅一次
    @PostConstruct
    public void init(){
        System.out.println("user servier init...");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("user servier destroy...");
    }

}
